﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Common
{
    /// <summary>
    /// 获取枚举
    /// </summary>
    public static class BaseGetEnum
    {
        public static List<object> GetCodeRulesList<T>()
        {
            var list = Enum.GetValues(typeof(T))
                .Cast<T>()
                .Select(item =>
                {
                    var field = typeof(T).GetField(item.ToString());
                    var description = field.GetCustomAttribute<DescriptionAttribute>();
                    return new
                    {
                        Value = item,
                        Name = item.ToString(),
                        Des = description?.Description ?? item.ToString()
                    };
                })
                .ToList<object>();

            return list;
        }
        /// <summary>
        /// 获取枚举类型的描述信息，如果枚举值有 DescriptionAttribute 标记，则返回标记中的描述，否则返回枚举值的字符串表示。
        /// </summary>
        /// <param name="value">要获取描述信息的枚举值</param>
        /// <returns>枚举值的描述信息或字符串表示</returns>
        public static string GetDescription(this Enum? value)
        {
            if (value == null) return string.Empty;
            // 获取枚举类型的字段信息
            FieldInfo field = value.GetType().GetField(value.ToString());

            // 获取字段上的 DescriptionAttribute 自定义特性
            DescriptionAttribute attribute = field.GetCustomAttribute<DescriptionAttribute>();

            // 如果存在 DescriptionAttribute，则返回其描述信息，否则返回枚举值的字符串表示
            if (attribute != null)
            {
                return attribute.Description;
            }

            return value.ToString();
        }
        /// <summary>
        /// 枚举Type转字典
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static Dictionary<int, string> EnumToDictionary(this Type enumType)
        {
            Dictionary<int, string> dictionary = new Dictionary<int, string>();
            Type typeDescription = typeof(DescriptionAttribute);
            FieldInfo[] fields = enumType.GetFields();
            int sValue = 0;
            string sText = string.Empty;
            foreach (FieldInfo field in fields)
            {
                if (field.FieldType.IsEnum)
                {
                    sValue = (int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null);
                    object[] arr = field.GetCustomAttributes(typeDescription, true);
                    if (arr.Length > 0)
                    {
                        DescriptionAttribute da = (DescriptionAttribute)arr[0];
                        sText = da.Description;
                    }
                    else
                    {
                        sText = field.Name;
                    }
                    dictionary.Add(sValue, sText);
                }
            }
            return dictionary;
        }

    }
}
